<?php
/*
Plugin Name: wp-reader-wall
Plugin URI: http://www.storyday.com/html/y2009/2547_readers-wall-plug-in-released.html
Description: 显示评论者的头像,支持wp3.0
Author: <a href="http://www.storyday.com">cosbeta</a> 
Version: 3.0
*/


$exclude_emails = array('zyxfsky@gmail.com','me@joysboy.net');//希望被排除的email请添加到此数组中

$imgsize = 48;//头像大小，单位px


add_filter('the_content', 'wp_readers_wall_list');

function wp_readers_wall_list( $content ){
	global $imgsize ;
	global $exclude_emails;
	if( strpos($content, '[READERWALL-WEEK]')==false  
		&& 	strpos($content, '[READERWALL-MONTH]')==false
		&& 	strpos($content, '[READERWALL-SEASON]')==false
		&& 	strpos($content, '[READERWALL-YEAR]')==false  )
	return $content;	
	//get time span
	if( strpos($content, '[READERWALL-WEEK]') ) {
		$cur_time_span = date('Y-m-d H:i:s', strtotime('-1 week'));
		$option = 0;
	}
	elseif( strpos($content, '[READERWALL-MONTH]') ) {
		$cur_time_span = date('Y-m-d H:i:s', strtotime('-1 Month')) ;
		$option = 1;
	}
	elseif( strpos($content, '[READERWALL-SEASON]') ){
		$cur_time_span = date('Y-m-d H:i:s', strtotime('-3 Month'));
		$option = 2;
	}
	else{
		$cur_time_span = date('Y-m-d H:i:s', strtotime('-1 Year'));
		$option = 3;
	}
	global $wpdb;
	
	$tablecomments = $wpdb->comments;
		
	$request = "SELECT count(comment_ID) comment_nums,comment_author, comment_author_email,comment_author_url FROM $tablecomments  where comment_date>'".$cur_time_span."' AND comment_type='' AND comment_approved=1 group by comment_author_email order by count(comment_ID) DESC ";
	
	$comments = $wpdb->get_results($request);
  
    foreach ($comments as $comment) {
		if (in_array($comment->comment_author_email, $exclude_emails) )  continue;
		if( $comment->comment_author_url ) $url = $comment->comment_author_url;
		else $url="#";
		//$tmp = "<a href='".$url."' title='".$comment->comment_author." (".$comment->comment_nums.")'><img ref='imgcache4wordpress' width='".$imgsize ."' height='".$imgsize ."' src='http://www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($comment->comment_author_email) )."&size=".$imgsize ."&d=identicon&r=G' alt='".$comment->comment_author."' /></a>";
		$avatar = get_avatar( $comment, $imgsize, 'http://xfeng.me/wp-content/uploads/images/defaultavatar.jpg', $comment->comment_author );
		$avatar =  substr_replace($avatar, "img style=\"margin:5px 0px 0px 5px\"", 1, 3);
		$tmp = "<a href='".$url."' title='".$comment->comment_author." (".$comment->comment_nums.")' rel=\"external nofollow\" onClick=\"_gaq.push(['_trackEvent', 'OutgoLink', 'Goto Reader Link', '$url']);\" target=\"_blank\">".$avatar."</a>";
		$output .= $tmp; 
       }
	   
	
	$output = "<div id='readerswall'>".$output."</div>";
	
	
	
	if(!empty($output)){
		if( $option == 0 )
			$content = preg_replace("'\[READERWALL-WEEK\]'i", $output, $content);
		elseif( $option == 1 )
			$content = preg_replace("'\[READERWALL-MONTH\]'i", $output, $content);
		elseif( $option == 2 )
			$content = preg_replace("'\[READERWALL-SEASON\]'i", $output, $content);
		else
			$content = preg_replace("'\[READERWALL-YEAR\]'i", $output, $content);
		}
		
	return $content;
}



?>
